import request from '@/utils/request'
import { requestNew } from '@/utils/request'
import store from '@/stores/index'
// const env = require('../../config/' + process.env.ENV_CONFIG + '.env')
// 结算接口
export async function orderSettle(params) {
  const res = await request({
    url: '/v2/order/settle',
    method: 'post',
    data: params || {}
  })
  return res
}
// 下单接口
export async function orderAdd(params = {}) {
  if (store && store.state && store.state.extraData) {
    if (store.state.extraData.data && store.state.extraData.data.liveId) {
      params.data_src = 'blive|MiniProgram|' + store.state.extraData.data.liveId
    }
  }
  if (!params.data_src) {
    params.data_src = uni.getStorageSync('__PAGE_PLATFORM__') || 'H5'
    // #ifdef MP-WEIXIN
    params.data_src = 'MiniProgram'
    // #endif
  }
  console.log(params)
  const res = await request({
    url: '/v2/order/add',
    method: 'post',
    data: params || {}
  })
  return res
}
// 支付方式接口
export async function getPayChannel(params = {}) {
  if (store && store.state && store.state.extraData) {
    if (store.state.extraData.data && store.state.extraData.data.liveId) {
      params.data_src = 'blive|MiniProgram|' + store.state.extraData.data.liveId
    }
  }
  if (!params.data_src) {
    params.data_src = uni.getStorageSync('__PAGE_PLATFORM__') || 'H5'
    // #ifdef MP-WEIXIN
    params.data_src = 'MiniProgram'
    // #endif
  }
  console.log(params)
  const res = await request({
    url: '/order/pay-channel',
    method: 'post',
    data: params || {}
  })
  return res
}
// 支付接口
export async function orderPay(params = {}) {
  if (store && store.state && store.state.extraData) {
    if (store.state.extraData.data && store.state.extraData.data.liveId) {
      params.data_src = 'blive|MiniProgram|' + store.state.extraData.data.liveId
    }
  }
  if (!params.data_src) {
    params.data_src = uni.getStorageSync('__PAGE_PLATFORM__') || 'H5'
    // #ifdef MP-WEIXIN
    params.data_src = 'MiniProgram'
    // #endif
  }
  const newParams = JSON.stringify(params)
  const res = await request({
    url: '/pay',
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    data: JSON.parse(newParams)
  })
  return res
}
// #ifdef H5
// 公众号支付接口
export function orderPayWechat(params) {
  const apiLink = location.href
  let link = ''
  if (params.wechatUrl) {
    link = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc6e2ab3530fea6a7&redirect_uri=${encodeURIComponent(apiLink)}&response_type=code&scope=snsapi_base&connect_redirect=1#wechat_redirect`
  } else {
    link = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc6e2ab3530fea6a7&redirect_uri=${encodeURIComponent(apiLink)}&response_type=code&scope=snsapi_base`
  }
  console.log('link', link)
  // location.href = link
  location.replace(link)
  return link
}
// #endif
// #ifdef H5
// 获取公众号code
export function getWeChatCode() {
  const apiLink = location.href
  console.log(apiLink);
  const link = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc6e2ab3530fea6a7&redirect_uri=${encodeURIComponent(apiLink)}&response_type=code&scope=snsapi_base&connect_redirect=1#wechat_redirect`
  console.log(link);
  location.replace(link)
  return link
}
// #endif

// 获取公众号open_id
export async function getPayOpenId(params) {
  const res = await request({
    url: '/wechat-official-account-openId/get',
    method: 'get',
    params: params || {}
  })
  return res
}
//获取小程序openid
export async function getMiniProgramOpenId(params) {
  const res = await request({
    url: '/get-mini-program-openid',
    method: 'get',
    params: params || {}
  })
  return res
}

// 订单搜索历史
export async function getSearchHistory(params) {
  const res = await request({
    url: '/order/search/his',
    method: 'get',
    params: params || {}
  })
  return res
}
// 清空订单搜索历史
export async function searchHistoryClear(params) {
  const res = await request({
    url: '/order/search/clear',
    method: 'get',
    params: params || {}
  })
  return res
}
// 订单列表接口
export async function getOrderList(params) {
  const res = await request({
    url: '/order',
    method: 'POST',
    data: params || {}
  })
  return res
}
// 订单详情接口
export async function getOrderDetail(params) {
  const res = await request({
    url: '/order/detail',
    method: 'POST',
    data: params || {}
  })
  return res
}
// 套餐商品列表接口
export async function getPackList(params) {
  const res = await request({
    url: '/order/goods/pack',
    method: 'POST',
    data: params || {}
  })
  return res
}
// 物流详情接口
export async function getOrderExpress(params) {
  const res = await request({
    url: '/order/express',
    method: 'POST',
    data: params || {}
  })
  return res
}

// 取消订单接口
export async function orderCancel(params) {
  const res = await request({
    url: '/order/cancel',
    method: 'POST',
    data: params || {}
  })
  return res
}

// 确认收货接口
export async function orderConfirm(params) {
  const res = await request({
    url: '/order/confirm-receipt',
    method: 'post',
    data: params || {}
  })
  return res
}

// 个人发票接口
export async function invoicePersonal(params) {
  const res = await request({
    url: '/order/invoice/personal',
    method: 'POST',
    data: params || {}
  })
  return res
}

// 增值税发票接口
export async function invoiceCompany(params) {
  const res = await request({
    url: '/order/invoice/company',
    method: 'POST',
    data: params || {}
  })
  return res
}

// 检验商品是否可以购买
export async function goodsValidate(params) {
  const res = await request({
    url: '/order/goods/validate',
    method: 'POST',
    data: params || {}
  })
  return res
}

// 商品评价列表
export async function orderRate(params) {
  const res = await request({
    url: '/order-comment/findOrderGoods',
    method: 'get',
    params: params || {}
  })
  return res
}

// 添加商品评价
export async function orderRateAdd(params) {
  const res = await request({
    url: '/order-comment/submit',
    method: 'POST',
    data: params || {}
  })
  return res
}

// 获取订单商品评价
export async function orderRatGet(params) {
  const res = await request({
    url: '/order-comment/findModifyComment',
    method: 'get',
    params: params || {}
  })
  return res
}

// 获取支付结果 /order/pay/result/{order_no}
export async function getOrderResult(params) {
  const res = await requestNew({
    url: '/order/pay/result/' + params.order_no,
    method: 'get'
  })
  return res
}

// 获取支付结果 /order/pay/result/{order_no}
export async function getOrderPaySuccess(params) {
  const res = await requestNew({
    url: '/gift/paySuccess',
    method: 'get',
    params
  })
  return res
}

/**
 * @Author: wjk
 * @Date: 2020-03-17 10:36:11
 * @LastEditors: wjk
 * @description: 删除购物袋指定商品
 */
export async function delLiveBag(data = {}) {
  const res = await requestNew({
    url: `/live-bx-app-api/live-bag/del/${data.liveId}`,
    method: 'post',
    data,
    headers: {
      showLoading: false
    }
  })
  return res
}

//判断验证码是否正确
export async function checkVerifyCode(data = {}) {
  const res = await request({
    url: '/order/pay/verify-code',
    method: 'POST',
    data: data || {}
  })
  return res
}
//判断交易密码是否正确
export async function checkVerifyDealPassword(data = {}) {
  const res = await request({
    url: '/order/pay/verify-password',
    method: 'POST',
    data: data || {},
    headers: {
      showMessage: false
    }
  })
  return res
}